package com.example.exercise.demo.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;

import com.example.exercise.demo.vo.AppDiscussVO;
import com.example.exercise.demo.dao.entity.AppDiscussEntity;
import com.example.exercise.demo.dao.AppDiscussDAO;

/**
* 
*/
@Service
public class AppDiscussService {

    @Autowired
    private AppDiscussDAO appDiscussDAO;

    public List<AppDiscussVO> findByWhere(Page<?> page, AppDiscussVO appDiscussVO){
        List<AppDiscussVO> list = new ArrayList<>();
        List<AppDiscussEntity> list0 = appDiscussDAO.findByWhere(page, appDiscussVO);
        for (AppDiscussEntity appDiscussEntity : list0) {
            list.add(toAppDiscussVO(appDiscussEntity));
        }
        return list;
    }

    public AppDiscussVO findByKey(AppDiscussVO appDiscussVO){
        AppDiscussEntity appDiscussEntity = appDiscussDAO.findByKey(appDiscussVO);
        return toAppDiscussVO(appDiscussEntity);
    }

    public int insert(AppDiscussVO appDiscussVO){
        return appDiscussDAO.insert(toAppDiscussEntity(appDiscussVO));
    }

    public int updateByKey(AppDiscussVO appDiscussVO){
        return appDiscussDAO.updateByKey(toAppDiscussEntity(appDiscussVO));
    }

    public int deleteByKey(AppDiscussVO appDiscussVO){
        return appDiscussDAO.deleteByKey(appDiscussVO);
    }

    private AppDiscussVO toAppDiscussVO(AppDiscussEntity appDiscussEntity) {
        AppDiscussVO appDiscussVO = new AppDiscussVO();
                appDiscussVO.setId(String.valueOf(appDiscussEntity.getId()));
        appDiscussVO.setUserId(String.valueOf(appDiscussEntity.getUserId()));
        appDiscussVO.setUserName(appDiscussEntity.getUserName());
        appDiscussVO.setPaperId(String.valueOf(appDiscussEntity.getPaperId()));
        appDiscussVO.setContext(appDiscussEntity.getContext());
        appDiscussVO.setCreatTime(String.valueOf(appDiscussEntity.getCreatTime()));

        return appDiscussVO;
    }

    private AppDiscussEntity toAppDiscussEntity(AppDiscussVO appDiscussVO) {
        AppDiscussEntity appDiscussEntity = new AppDiscussEntity();
                String id = appDiscussVO.getId();
        if(id != null) {
            appDiscussEntity.setId(Integer.parseInt(appDiscussVO.getId()));
        }
        String userId = appDiscussVO.getUserId();
        if(userId != null) {
            appDiscussEntity.setUserId(Integer.parseInt(appDiscussVO.getUserId()));
        }
        appDiscussEntity.setUserName(appDiscussVO.getUserName());
        String paperId = appDiscussVO.getPaperId();
        if(paperId != null) {
            appDiscussEntity.setPaperId(Integer.parseInt(appDiscussVO.getPaperId()));
        }
        appDiscussEntity.setContext(appDiscussVO.getContext());

        return appDiscussEntity;
    }

}